﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace GoShopping.Web.SystemManager
{
    public partial class CommentList : System.Web.UI.Page
    {
        BLL.SysInfo.RemarkPrdctBean rpb = new GoShopping.BLL.SysInfo.RemarkPrdctBean();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Master.PrdtButton.Visible = false;
                //Master.SupplierButton.Visible = false;
                //Master.ElementSelectedChange = GetTreeView;
                SetControlCommon.GetDDLType(ddlPrdtType);
                GetGVData(null);
            }
        }
        private void GetTreeView()
        {
            TreeView tv = Master.MasterTreeView;
            SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("~/ProductManager/Navigation.xml"), "sys");
        }
        protected void extGVPrdtComment_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.extGVPrdtComment.PageIndex = e.NewPageIndex;
            GetGVData(ddlPrdtType.SelectedValue);
        }

        protected void extGVPrdtComment_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "IsReply")
            {
                GetDVData(Convert.ToInt32(e.CommandArgument));
            }
            else if (e.CommandName == "Cover")
            {
                try
                {
                    rpb.CoverOneRemarkPrdct(Convert.ToInt32(e.CommandArgument), true);
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('屏蔽成功!')</script>", false);
                }
                catch
                { }
            }
        }

        protected void dtPrdtComment_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == "Reply")
            {
                try
                {
                    rpb.ReplyOneRemarkPrdct(Convert.ToInt32(e.CommandArgument), ((TextBox)dtPrdtComment.FindControl("zlTxtPrdtRemark")).Text, Context.User.Identity.Name);
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('回复成功!')</script>", false);
                }
                catch
                {

                }
            }
        }

        void GetGVData(string filterString)
        {
            if (string.IsNullOrEmpty(filterString))
            {
                extGVPrdtComment.DataSource = rpb.GetAllRemarkPrdct();
                extGVPrdtComment.DataBind();
            }
            else
            {
                extGVPrdtComment.DataSource = rpb.GetRemarkPrdctList("and ProductID in (SELECT ID FROM Product WHERE ProductTypeID=" + filterString + ")");
                extGVPrdtComment.DataBind();
            }
        }
        void GetDVData(int id)
        {
            dtPrdtComment.DataSource = rpb.GetRemarkPrdctList("and id=" + id);
            dtPrdtComment.DataBind();

            #region Temp
            //if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            //{
            //    string id = Request.QueryString["id"].ToString();
            //    dtPrdtComment.DataSource = rpb.GetRemarkPrdctList("and id=" + id);
            //    dtPrdtComment.DataBind();

            //    #region Temp
            //    //string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["GoShoppingConnectionString"].ConnectionString;// @"Data Source=RAY\SQLEXPRESS;Initial Catalog=GoShopping;Persist Security Info=True;User ID=sa;Password=1984326z";
            //    //System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(connStr);
            //    //System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand();
            //    //sqlCmd.Connection = sqlConn;
            //    //sqlConn.Open();
            //    //sqlCmd.CommandText = "select * from dbo.Advisory where id=" + id;
            //    ////sqlCmd.CommandText = "SELECT dbo.ProductType.ID, dbo.ProductType.TypeName, dbo.ProductType.ParentID, dbo.ProductType.Remark, + "
            //    ////          + "dbo.ProductType_RelateColumn.ProductTypeID, dbo.ProductType_RelateColumn.PropertyChName, "
            //    ////          + "dbo.ProductType_RelateColumn.PropertyColumnName, dbo.ProductType_RelateColumn.ColumnType, "
            //    ////          + "dbo.ProductType_RelateColumn.SelectTypeValue "
            //    ////          + "FROM dbo.ProductType INNER JOIN "
            //    ////          + " dbo.ProductType_RelateColumn ON dbo.ProductType.ID = dbo.ProductType_RelateColumn.ProductTypeID";
            //    //System.Data.SqlClient.SqlDataAdapter sqlDa = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
            //    //DataSet ds = new DataSet();
            //    //sqlDa.Fill(ds);
            //    //sqlConn.Close();
            //    #endregion
            //}
            #endregion
        }

        protected void ddlPrdtType_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetGVData(ddlPrdtType.SelectedValue);
        }
    }
}
